﻿

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md2">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 10px;">
                    <!-- 树工具栏 -->
                    <div class="layui-form toolbar crmlefttreetar" id="treeBar">
                        <div class="dropdown-menu">
                            <button class="layui-btn icon-btn layui-btn-primary" id="grpSelect" type="button">
                                分组<i class="layui-icon layui-icon-drop"></i>
                            </button>
                            <ul class="dropdown-menu-nav" style="width:200px">
                                <li class="dropdown-menu-item" data-name="user"><i class="layui-icon layui-icon-circle"></i><a class="">组织人员</a><span class="numsort"></span></li>
                                <li class="dropdown-menu-item" data-name="status"><i class="layui-icon layui-icon-circle"></i><a class="">状态</a><span class="numsort"></span></li>
                                <li class="dropdown-menu-item text-center " style="margin:10px 0;">
                                    <button class="layui-btn layui-btn-sm group-search" type="button" id="btn_grpsearch">确定</button>
                                    <button class="layui-btn layui-btn-sm layui-btn-primary group-default" type="button" id="btn_grpdefault">设为默认</button>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <!-- 左树 -->
                    <div class="crmlefttree" id="orgTree">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md10">
            <div class="layui-card">
                <div class="layui-card-body table-tool-mini full-table">
                    <div class="layui-form toolbar" id="tbToolBar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input id="txt_search" class="layui-input" type="text" placeholder="" style="width:300px;" />
                            </div>
                            <div class="layui-inline">
                                <div class="dropdown-menu">
                                    <button class="layui-btn icon-btn layui-btn-primary">
                                        &nbsp;筛选<i class="layui-icon layui-icon-drop"></i>
                                    </button>
                                    <ul class="dropdown-menu-nav">
                                        <li id="searchMoreClear"><a>清空查询</a></li>
                                        <li id="searchMore"><a>更多筛选</a></li>
                                    </ul>
                                </div>

                                <div class="dropdown-menu">
                                    <button class="layui-btn icon-btn layui-btn-primary">
                                        &nbsp;操作<i class="layui-icon layui-icon-drop"></i>
                                    </button>
                                    <ul class="dropdown-menu-nav">
                                        <li id="li_add"><a>新增</a></li>
                                        <li id="li_del"><a>删除</a></li>
                                    </ul>
                                </div>
                            </div>
                            
                    </div>
                    <table class="layui-table" id="table" lay-filter="table"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<!--更多查询表单-->
<script type="text/html" id="searchModel">
    <form id="searchModelForm" lay-filter="searchModelForm" class="layui-form model-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">订单编号</label>
                <div class="layui-input-block">
                    <input name="OrderNo" placeholder="" type="text" class="layui-input" maxlength="30" autocomplete="off" />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">创建时间:</label>
                <div class="layui-input-inline" style="width: 238px;">
                    <input type="text" id="beginTime" name="beginTime" placeholder="请选择开始日期" autocomplete="off" class="layui-input" lay-filter="beginTime" />
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width:238px;">
                    <input type="text" id="endTime" name="endTime" placeholder="请选择结束日期" autocomplete="off" class="layui-input" lay-filter="endTime" />
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">关闭</button>
            <button class="layui-btn" lay-filter="SearchModelSubmit" lay-submit>查询</button>
        </div>
    </form>
</script>


<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'setter', 'tree', 'errorCode', 'laytpl', 'laydate', 'treeTable'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var setter = layui.setter;
        var errorCode = layui.errorCode;
        var tree = layui.tree;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var dataShowType = 'table';//当前数据展示模式  默认为列表
        var treeRender;
        var treeSearchJson;
        var moreSearchJson
        treeRender = function () {
        var loadindex=layer.load(0);
            admin.req(setter.SysNResourceService + 'NResource_ExpressAddress/GetTreeData', JSON.stringify({ DataGroup: DataGroup }), function (res) {
                layer.close(loadindex);
                var treeData = layui.treeTable.pidToChildren(res.data, 'id', 'parentid');
                tree.render({
                    elem: '#orgTree',
                    onlyIconControl: true,
                    data: treeData,
                    click: function (obj) {
                        $('#orgTree').find('.layui-tree-txt').removeClass('lefttreeclick');
                        $(obj.elem).find('.layui-tree-txt:eq(0)').addClass('lefttreeclick');

                        var data = obj.data;
                        treeSearchJson = data.id;
                        searchParam = { searchKey: $('#txt_search').val(), searchJson: moreSearchJson, treeSearchJson: treeSearchJson };
                        insTb.reload({ where: searchParam }, 'data');
                    }
                });
            }, 'post');
        }
        // 渲染表格
        var insTb = table.render({
            elem: '#table',
            method: 'post',
            height: 'full-150',
            size: 'sm',
            even: true,
            url: setter.SysNResourceService + 'NResource_ExpressAddress/GetList',
            page: {
                limit: 50,
                limits: [20, 50, 100, 200],
                groups: 10
            },
            headers: { 'Authorization': setter.getToken() },
            contentType: 'application/json',
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                                                                                                                                                                                                                                                                            { field: 'CompayName', sort: true, title: 'CompayName',width:100  },
		                                                                { field: 'LinkPerson', sort: true, title: 'LinkPerson',width:100  },
		                                                                { field: 'Address', sort: true, title: 'Address',width:100  },
		                                                                { field: 'LinkMobile', sort: true, title: 'LinkMobile',width:100  },
		                                            {
                    field: 'CreateTime', sort: true, templet: function (d) {
                        return util.toDateString(d.CreateTime);
                    }, title: '创建时间'
                }
            ]],
            done: function (res, curr, count) {
                $('.layui-table').css("width", "100%");
                $("th[data-field='bar']").css("border-right", 'none');
            }
        });
        $('#li_add').click(function () {
           admin.open({
                type: 1,
                offset: '0px',
                area: [$(window).width() + 'px', $(window).height() + 'px'],
                title: '新增',
                data: { insTb: insTb, seeType: 1, treeRender: treeRender },
                url: 'components/NResource/NResource_ExpressAddress/edit.html',
                success: function (bzlayero, bzdIndex) {
                    //layer.full(bzdIndex);
                }
            });
        });
        table.on('rowDouble(table)', function (obj) {
            var model = obj.data;
            admin.open({
                type: 1,
                offset: '0px',
                area: [$(window).width() + 'px', $(window).height() + 'px'],
                title: '查看',
                data: { Id: model.Id, insTb: insTb, seeType: 2, detailEnt: model, treeRender: treeRender },
                url: 'components/NResource/NResource_ExpressAddress/edit.html',
                success: function (bzlayero, bzdIndex) {
                    //layer.full(bzdIndex);
                }
            });
        });

        $('#li_del').click(function () {
            var ids = new Array();
            var checkList = table.checkStatus('table').data;
            for (var i = 0; i < checkList.length; i++) {
                ids.push(checkList[i].Id);
            }
            if (ids.length == 0) {
                layer.msg("请勾选需要删除的明细！");
                return;
            }
            layer.confirm('确定要删除所选的明细吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req(setter.SysNResourceService + 'NResource_ExpressAddress/Del', JSON.stringify({ strEntity: JSON.stringify(ids) }), function (res) {
                    layer.closeAll('loading');
                    layer.msg("删除成功。", { icon: 1 });
                    insTb.reload({}, 'data');
                    treeRender();
                }, 'post');
            });
        });
       

        $("#searchMoreClear").click(function () {
            $("#txt_search").val("");
            moreSearchJson = "";
            treeSearchJson = "";
            insTb.reload({ where: { searchKey: '', searchJson: moreSearchJson, treeSearchJson: treeSearchJson }, page: { curr: 1 } }, 'data');
        });
        $("#searchMore").click(function () {
            admin.open({
                type: 6,
                fixed: true,  // 加这个可解决没有居中的问题
                area: ['auto', 'auto'],
                title: "更多筛选",
                content: $('#searchModel').html(),
                success: function (layero, dIndex) {
                    form.render('select');
                    //日期
                    laydate.render({
                        elem: '#beginTime',
                        trigger: 'click'
                    });
                    //日期
                    laydate.render({
                        elem: '#endTime',
                        trigger: 'click'
                    });

                    if (moreSearchJson != '' && moreSearchJson != undefined) {
                       form.val('searchModelForm', JSON.parse(moreSearchJson));
                    }

                    form.on('submit(SearchModelSubmit)', function (data) {
                        var DetailNo = data.field.DetailNo;
                        var OrderNo = data.field.OrderNo;
                        var OuterScope = data.field.OuterScope;
                        var beginTime = data.field.beginTime == "" ? null : data.field.beginTime+ ' 00:00:00';
                        var endTime = data.field.endTime == "" ? null : data.field.endTime+ ' 23:59:59';
                        moreSearchJson = JSON.stringify({ DetailNo: DetailNo, OrderNo: OrderNo, OuterScope: OuterScope, beginTime: beginTime, endTime: endTime });
                        insTb.reload({ where: { searchKey: $('#txt_search').val(), searchJson: moreSearchJson, treeSearchJson: treeSearchJson }, page: { curr: 1 } }, 'data');
                        admin.closeDialog('#searchModelForm');
                        return false;
                    });
                }
            });
        })
        // 搜索
        $('#txt_search').keyup(function (event) {
            if (event.keyCode == 13) {
                insTb.reload({ where: { searchKey: $("#txt_search").val(), searchJson: moreSearchJson, treeSearchJson: treeSearchJson } }, 'data');
            }
        });

        $('#btn_grpdefault').click(function () {
            if (DataGroup.length == 0) {
                layer.msg("请至少选择一个分组!");
                 sort = 1;
                return false;
            }
            sort = DataGroup.length + 2;
            $('#grpSelect').click();
            admin.syncreq(setter.SysPurchaseService + 'Purchase_LeftGroup/Save', JSON.stringify({ key: 'NResource_ExpressAddress', GroupJson: JSON.stringify(DataGroup) }), function (res) {
                treeRender();
            }, 'post');
        });

        $('#btn_grpsearch').click(function () {
            if (DataGroup.length == 0) {
                layer.msg("请至少选择一个分组!");
                 sort = 1;
                return false;
            }
            $('#grpSelect').click();
            sort = DataGroup.length + 2;
            treeRender();
        });
        //从后台读取保存左侧树的数据
        var DataGroup = new Array();
        var sort = 1;
        admin.syncreq(setter.SysPurchaseService + 'Purchase_LeftGroup/GetLeftGroup', JSON.stringify({ key: 'NResource_ExpressAddress' }), function (res) {
            if (res.data != "") {
                var arr = JSON.parse(res.data);
                for (var i = 0; i < arr.length; i++) {
                   $(".dropdown-menu-item[data-name='" + arr[i].split(":")[1] + "']").find("span").text(i + 1);
                    $(".dropdown-menu-item[data-name='" + arr[i].split(":")[1] + "']").find('i').removeClass('layui-icon-circle');
                    $(".dropdown-menu-item[data-name='" + arr[i].split(":")[1] + "']").find('i').addClass('layui-icon-ok-circle');
                    DataGroup.push(arr[i]);
                }
                sort = arr.length + 1;
            }
            treeRender();
        }, 'post');
        $('.dropdown-menu-item').click(function () {
            if ($(this).find('i').hasClass('layui-icon-circle')) {
                $(this).find('i').removeClass('layui-icon-circle');
                $(this).find('i').addClass('layui-icon-ok-circle');
                $(this).find("span").text(sort);

                DataGroup.push(sort + ":" + $(this).attr("data-name"));
                sort++;

            }
            else {
                $(this).find('i').removeClass('layui-icon-ok-circle');
                $(this).find('i').addClass('layui-icon-circle');
                var spantext = $(this).find("span").text();
                $(this).find("span").text('');
                sort--;
                for (var i = 0; i < DataGroup.length; i++) {
                    console.log((spantext + ":" + $(this).attr("data-name")));
                    if (DataGroup[i] == (spantext + ":" + $(this).attr("data-name"))) {
                        DataGroup.splice(i, 1);
                        break;
                    }
                }
                for (var i = 0; i < DataGroup.length; i++) {
                    $(".dropdown-menu-item[data-name='" + DataGroup[i].split(":")[1] + "']").find("span").text(i + 1);
                    DataGroup[i] = i + 1 + ":" + DataGroup[i].split(":")[1];
                }
            }
        });

        
    });
</script>
